package src.com.client.view;

import src.com.client.service.FileClientService;
import src.com.client.service.MessageClientService;
import src.com.client.service.UserClientService;
import src.com.client.utility.Utility;

/**
 * 客户端的菜单界面
 */
public class QQView {
    private boolean loop = true; // 控制是否显示菜单
    private String key = ""; // 接受用户的键盘输入
    private MessageClientService messageClientService = new MessageClientService(); // 用于发送私聊消息
    private UserClientService userClientService = new UserClientService(); // 对象是用于登录服务/注册用户
    private FileClientService fileClientService = new FileClientService(); // 用于传输文件

    public static void main(String[] args) {
        new QQView().mainMenu();
    }
    // 显示主菜单
    private void mainMenu() {
        while (loop) {
            System.out.println("=========================欢迎登陆网络通信系统=========================");
            System.out.println("\t\t\t\t1 登录系统");
            System.out.println("\t\t\t\t9 退出系统");
            System.out.print("please enter your choice: ");

            key = Utility.readString(1);

            // 根据用户的输入，来处理不同的逻辑
            switch (key) {
                case "1":
                    System.out.print("please enter UserID: ");
                    String userId = Utility.readString(50);
                    System.out.print("please enter Password: ");
                    String password = Utility.readString(50);
                    // 此处应该去服务器验证
                    if (userClientService.checkUser(userId, password)) { // 还没有写完 先写个大致的逻辑
                        System.out.println("=========================欢迎（用户：" + userId + "）登录成功=========================");
                        // 进入二级菜单
                        while (loop) {
                            System.out.println("\n===================网络通信系统二级菜单（用户：" + userId + "）===================");
                            System.out.println("\t\t\t\t 1 显示在线用户列表");
                            System.out.println("\t\t\t\t 2 群发消息");
                            System.out.println("\t\t\t\t 3 私聊消息");
                            System.out.println("\t\t\t\t 4 发送文件");
                            System.out.println("\t\t\t\t 9 退出系统");
                            System.out.print("please enter your choice: ");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    System.out.println("显示在线用户列表");
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.println("群发消息");
                                    System.out.print("请输入想要对大家说的话：");
                                    String toAllContent = Utility.readString(100);
                                    messageClientService.sendMessageToAll(toAllContent, userId);
                                    break;
                                case "3":
                                    System.out.println("私聊消息");
                                    System.out.print("请输入想聊天的用户id：");
                                    String getterId = Utility.readString(50);
                                    System.out.print("请输入想聊天的内容：");
                                    String getContent = Utility.readString(100);
                                    // 编写一个方法 将以上待发送消息 发给服务端
                                    messageClientService.sendMessageToOne(getContent, userId, getterId);
                                    break;
                                case "4":
                                    System.out.println("发送文件");
                                    System.out.print("请输入待发送文件的目标用户：");
                                    getterId = Utility.readString(50);
                                    System.out.print("请输入待发送文件的源路径(d:\\xx.jpg)：");
                                    String src = Utility.readString(100);
                                    System.out.print("请输入待发送文件的目标路径(d:\\xx.jpg)：");
                                    String des = Utility.readString(100);
                                    fileClientService.sendFileToOne(src, des, userId, getterId);
                                    break;
                                case "9":
                                    // 调用方法 给服务器发送一个退出系统的Message对象
                                    userClientService.logOut();
                                    loop = false;
                                    break;
                            }
                        }
                    } else {
                        System.out.println("登陆失败！");
                    }
                    break;
                case "9":

                    loop = false;
                    break;

            }
        }
    }
}
